package com.xinziruo.richclient.util.filecommand.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.xinziruo.richclient.util.ExceptionUtils;
import com.xinziruo.richclient.util.IOUtils;
import com.xinziruo.richclient.util.filecommand.FileCommand;



/**
 * 
 * 
 * @author loudyn.
 */
public class CopyFileCommand implements FileCommand {
	private final File source;
	private final File target;

	/**
	 * 
	 * @param source
	 * @param target
	 */
	public CopyFileCommand(String source, String target) {
		this(new File(source), new File(target));
	}

	/**
	 * 
	 * @param source
	 * @param target
	 */
	public CopyFileCommand(File source, File target) {
		this.source = source;
		this.target = target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.module.util.filecommand.FileCommand#execute()
	 */
	public void execute() {
		InputStream in = null;
		OutputStream out = null;

		try {

			in = new FileInputStream(source);
			out = new FileOutputStream(target);
			IOUtils.piping(in, out);

		} catch (Exception e) {
			throw ExceptionUtils.toUnchecked(e);
		} finally {
			IOUtils.freeQuietly(in, out);
		}
	}
}
